package com.cougil.uoc.seghismed.webservice.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.cougil.uoc.seghismed.model.ciphed.test.Animal;
import com.cougil.uoc.seghismed.model.ciphed.test.AnimalImpl;
import com.cougil.uoc.seghismed.model.ciphed.test.ObjectFactory;
import com.cougil.uoc.seghismed.model.ciphed.test.TestCiphedBytes1;
import com.cougil.uoc.seghismed.model.ciphed.test.TestCiphedBytes2;
import com.cougil.uoc.seghismed.model.ciphed.test.TipusXifrat2;
import com.cougil.uoc.seghismed.model.ciphed.test.UAListStruct;
import com.cougil.uoc.seghismed.model.ciphed.test.UANoXML;
import com.cougil.uoc.seghismed.utils.SeghismedUtils;
import com.cougil.uoc.seghismed.webservice.TestWebService;

/**
 * Classe que implementa el servei web ofert/consumit de test/proves
 *
 * @author Ignacio Cougil Jares
 *
 */
@WebService(
		endpointInterface = "com.cougil.uoc.seghismed.webservice.TestWebService",
		serviceName = "TestWebService"
)
public class TestWebServiceImpl implements TestWebService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(TestWebServiceImpl.class);

	/*
	 * (non-Javadoc)
	 *
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#getDate(byte[])
	 */
	@Override
	public Date getDate(byte[] bytes) {
		String s = new String(bytes);
		TestWebServiceImpl.logger.debug("Server --> Received: " + s);
		Date d = new Date();
		TestWebServiceImpl.logger.debug("Server --> Returned: " + d);
		return d;
	}

	private Byte[] getLlista(String cadena) {
		byte[] bits = cadena.getBytes();
		Byte[] bytes = new Byte[bits.length];
		for (int i = 0; i < bits.length; i++) {
			bytes[i] = new Byte(bits[i]);
		}
		return bytes;
	}

	private String getTipusDesXifrat(Byte[] valor) {
		byte[] bytes = new byte[valor.length];
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = valor[i].byteValue();
		}
		return new String(bytes);
	}


	private String getTipusDesXifrat(List<Byte> valorXifrat) {
		Byte[] bytes = valorXifrat.toArray(new Byte[0]);
		return this.getTipusDesXifrat(bytes);
	}

	private TipusXifrat2 getTipusXifrat(String cadena) {
		TipusXifrat2 tx = new TipusXifrat2();
		Byte[] array = this.getLlista(cadena);
		List<Byte> llista = Arrays.asList(array);
		tx.setValorXifrat(llista);
		return tx;
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#getUA(com.cougil.uoc.seghismed.model.ciphed.test.UAListStruct)
	 */
	@Override
	public UAListStruct getUA(UAListStruct uaRec) {
		TestWebServiceImpl.logger.debug("Server --> Received: " + uaRec + " que és: [" + this.getUAString(uaRec) + "]");
		String cadena = "demorenowerl!";
		UAListStruct uaSend = new UAListStruct();
		UANoXML uaNoXML = new UANoXML();
		uaNoXML.setName("Bart");
		uaNoXML.setValor("maduixes".getBytes());
		uaSend.setNoXML(uaNoXML);
		uaSend.setUA(cadena.getBytes());
		TestWebServiceImpl.logger.debug("Server --> Returned: " + uaSend + " que és: [" + new String(uaSend.getUA())
				+ uaNoXML + "]");
		return uaSend;
	}

	private String getUAString(UAListStruct ua) {
		return new String(ua.getUA()) + ua.getNoXML();
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#gimmeAnAnimal(com.cougil.uoc.seghismed.model.ciphed.test.Animal)
	 */
	@Override
	public Animal gimmeAnAnimal(Animal rebut) {
		Animal rec = new AnimalImpl();
		BeanUtils.copyProperties(rebut, rec);
		TestWebServiceImpl.logger.debug("gimmeAnAnimal received >>: " + rec);
		AnimalImpl newAnimal = new AnimalImpl();
		newAnimal.setApodo("kurrupipi".getBytes());
		newAnimal.setClase("perruno");
		newAnimal.setName("Toni");
		TestWebServiceImpl.logger.debug("gimmeAnAnimal returned >>: " + newAnimal);
		return newAnimal;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#gimmeCiphed1(com.cougil.uoc.seghismed.model.ciphed.test.TestCiphedBytes1)
	 */
	@Override
	public TestCiphedBytes1 gimmeCiphed1(TestCiphedBytes1 test) {
		TestWebServiceImpl.logger.debug("Server --> Received: " + test + " que és: [" + test + "]");
		ObjectFactory oj = new ObjectFactory();
		TestCiphedBytes1 newTest = new TestCiphedBytes1();
		//newTest.setNomOperacioCiphed(getLlista(newTest.getNomOperacioCiphed(), "exemple"));
		// logger.debug("Server --> Returned: " + newTest + " que és: ["
//				+ getTipusDesXifrat(newTest.getNomOperacioCiphed())
//				+ "]");
		return newTest;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#gimmeCiphed2(com.cougil.uoc.seghismed.model.ciphed.test.TestCiphedBytes2)
	 */
	@Override
	public TestCiphedBytes2 gimmeCiphed2(TestCiphedBytes2 test) {
		TestWebServiceImpl.logger.debug("Server --> Received: " + test + " que és: ["
				+ this.getTipusDesXifrat(test.getNomOperacioCiphed().getValorXifrat()) + "]");
		ObjectFactory oj = new ObjectFactory();
		TestCiphedBytes2 newTest = oj.createTestCiphedBytes2();
		String cadena = "nova cadena a retornar! amb caràcters estranys!";
		// + SeghismedUtils.getRandomString(5) + "]";
		newTest.setNomOperacioCiphed(this.getTipusXifrat(cadena));
		TestWebServiceImpl.logger.debug("Server --> Returned: " + newTest + " que és: ["
				+ this.getTipusDesXifrat(newTest.getNomOperacioCiphed().getValorXifrat())
				+ "]");
		return newTest;
	}

	/* (non-Javadoc)
	 * @see com.cougil.uoc.seghismed.webservice.TestWebService#test(java.lang.String)
	 */
	@Override
	public String test(String info) {
		TestWebServiceImpl.logger.debug("Server --> Received: " + info);
		String returned = info+" - "+SeghismedUtils.getRandomDate();
		TestWebServiceImpl.logger.debug("Server --> Returned: " + returned);
		return returned;
	}


}
